package SparkStreaming

import org.apache.spark._
import org.apache.spark.streaming._


object Streaming {
  def main(args: Array[String]): Unit = {
    //创建StreamingContext
    val conf = new SparkConf()
      .setMaster("local[2]")  //两个线程
      .setAppName("NetworkWordCount")
    //按照时间间隔为1秒钟切分数据流
    val ssc = new StreamingContext(conf, Seconds(1))

    //创建DStream
    val lines = ssc.socketTextStream("localhost", 9999)

    //操作DStream
    val words = lines.flatMap(_.split(""))
    val pairs = words.map(word => (word, 1))
    val wordCounts = pairs.reduceByKey(_+_)  //将此DStream中的每个RDD的前10个元素打印到控制台
    wordCounts.print()

    //启动Spark Streaming
    //开始计算
    ssc.start()
    //等待计算结束
    ssc.awaitTermination()

  }
}
